package com.example.demo.aspects;

import com.example.demo.annotation.OperSysLog;
import com.example.demo.dao.FcSysLogDao;
import com.example.demo.pojo.FcSysLogDO;
import com.example.demo.utils.TinyUUIDGenerate;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.Date;

@Aspect
@Component
public class OperLogAspect {

    @Resource
    FcSysLogDao fcSysLogDao;
    @Pointcut("@annotation(com.example.demo.annotation.OperSysLog)")
    public void OperLogCut()
    {

    }
    @AfterReturning(value = "OperLogCut()")
    public void OperLogFun(JoinPoint joinPoint) throws UnsupportedEncodingException {
        //注解中的三个值
        String operModul = "";
        String operType = "";
        String operDesc = "";

        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        OperSysLog operSysLog = method.getAnnotation(OperSysLog.class);
        operModul = operSysLog.operModul();
        operDesc = operSysLog.operDesc();
        operType = operSysLog.operType();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String nickName = request.getHeader("nickName");
        String userId = request.getHeader("userId");
        if(StringUtils.isNotEmpty(nickName)){
            nickName = URLDecoder.decode(nickName,"UTF-8");
        }
        FcSysLogDO sysLog = new FcSysLogDO();
        sysLog.setClientIp(getIpAddr(request));
        sysLog.setSysLogId(TinyUUIDGenerate.generate());
        sysLog.setCreateBy(nickName);
        sysLog.setOperType(operType);
        sysLog.setOperDesc(operDesc);
        sysLog.setModule(operModul);
        if(StringUtils.isNotEmpty(userId)){
            sysLog.setUserId(userId);
        }
        Date date = new Date();
        sysLog.setCreateTime(date);
        sysLog.setVerison(1);
        sysLog.setIsDelete(00);
        fcSysLogDao.insert(sysLog);
    }

    public static String getIpAddr(HttpServletRequest request)
    {
        if (request == null)
        {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
}
